package cn.wolfcode.shop.cloud.mq;

import cn.wolfcode.shop.cloud.service.ISeckillGoodService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RocketMQMessageListener(
        consumerGroup = MQConstans.CREATE_ORDER_FAIL_GROUP,
        topic = MQConstans.CREATE_ORDER,
        selectorExpression = MQConstans.CREATE_ORDER_FAIL_TAG
)
public class OrderFailMQListener implements RocketMQListener<Map> {
    @Autowired
    private ISeckillGoodService seckillGoodService;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void onMessage(Map map) {

        System.out.println("进入到失败的消费消息");
        Object seckillId = map.get("seckillId");
        Long goodId = null;
        if (seckillId instanceof Long) {
            goodId = (Long) seckillId;
        } else if (seckillId instanceof Integer) {
            goodId = ((Integer) seckillId).longValue();
        }
        //回补预库存
        seckillGoodService.syncPreCount(goodId);
        //修改本地表示
        rocketMQTemplate.syncSend(MQConstans.CREATE_ORDER_BRDER_DEST, goodId);
    }
}
